package com.heima.file.config;


import com.heima.file.service.FileStorageService;
import com.heima.file.service.impl.MinIOFileStorageService;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


@Configuration
// 下面这个 确保上面 那个配置类能使用。
@EnableConfigurationProperties(MinIOConfigProperties.class)
public class MinIOAutoConfiguration {


    @Resource
    private MinIOConfigProperties minIOConfigProperties;

//    @Bean
//    @ConditionalOnMissingBean
//    public MinioClient buildMinioClient() {
//        return MinioClient
//                .builder()
//                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
//                .endpoint(minIOConfigProperties.getEndpoint())
//                .build();
//    }

//    仿照 测试类里面的写法进行书写。
    @Bean
    @ConditionalOnMissingBean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .credentials(minIOConfigProperties.getAccessKey(),minIOConfigProperties.getSecretKey())
                .endpoint(minIOConfigProperties.getEndpoint())
                .build();
    }

    @Bean
    @ConditionalOnMissingBean
    public FileStorageService fileStorageService(MinioClient minioClient) {
        return new MinIOFileStorageService(minioClient, this.minIOConfigProperties);
    }
}
